#!/usr/bin/env python
import gconf
import os
from pysqlite2 import dbapi2 as sqlite
__gconf_client = gconf.client_get_default()
 
def set_default_user(username, system):    
    __gconf_client.set_string('/apps/dieu/default_username', username + ':' + system)

def get_default_user():
    return __gconf_client.get_string('/apps/dieu/default_username')

def get_users():
    return __gconf_client.get_list('/apps/dieu/usernames', 'string')

def check_for_dieu_dir():
    if os.path.exists(os.environ['HOME'] + '/.config/dieu/'):
        pass
    else:
        os.makedirs(os.environ['HOME'] + '/.config/dieu/')

def create_user_database():
    check_for_dieu_dir()
    __dieu_database = sqlite.connect(os.environ['HOME'] + '/.config/dieu/dieu.db')
    __dieu_database_cursor = __dieu_database.cursor()
    __dieu_database_cursor.execute('CREATE TABLE Users (id INTEGER PRIMARY KEY, Username VARCHAR(50), Password VARCHAR(50), System VARCHAR(50))')

def create_user(username, password, system):
    try: create_user_database()
    except sqlite.OperationalError:
        pass
    __dieu_database = sqlite.connect(os.environ['HOME'] + '/.config/dieu/dieu.db')
    __dieu_database_cursor = __dieu_database.cursor()
    __dieu_database_cursor.execute('INSERT INTO Users VALUES (null, ?, ?, ?)', (username, password, system))
    __dieu_database.commit()
    usernames_list = __gconf_client.get_list('/apps/dieu/usernames', 'string')
    usernames_list.append(username + ':' + system)
    __gconf_client.set_list('/apps/dieu/usernames', 'string', usernames_list)

def delete_user(username, system):
    __dieu_database = sqlite.connect(os.environ['HOME'] + '/.config/dieu/dieu.db')
    __dieu_database_cursor = __dieu_database_cursor()
        
def get_password(username, system):
    __dieu_database = sqlite.connect(os.environ['HOME'] + '/.config/dieu/dieu.db')
    __dieu_database_cursor = __dieu_database.cursor()
    __dieu_database_cursor.execute('SELECT * FROM Users')
    users = __dieu_database_cursor.fetchall()
    for item in users:
        if item[1] == username:
            if item[3] == system:
                return item[2]

def set_libnotify(quoi):
    if quoi == True:
        __gconf_client.set_bool('/apps/dieu/use_libnotify', True)
    else:
        __gconf_client.set_bool('/apps/dieu/use_libnotify', False)

def get_libnotify():
    return __gconf_client.get_bool('/apps/dieu/use_libnotify')

def set_recent(quoi):
    if quoi == True:
        __gconf_client.set_bool('/apps/dieu/display_recent', True)
    else:
        __gconf_client.set_bool('/apps/dieu/display_recent', False)

def get_recent():
    return __gconf_client.get_bool('/apps/dieu/display_recent')

def set_show_all(quoi):
    if quoi == True:
        __gconf_client.set_bool('/apps/dieu/display_all', True)
    else:
        __gconf_client.set_bool('/apps/dieu/display_all', False)

def get_show_all():
    return __gconf_client.get_bool('/apps/dieu/display_all')

def set_status(user, system, status):
    __gconf_client.set_string('/apps/dieu/'+user+':'+system+'_status', status)

def get_status(user, system):
    return __gconf_client.get_string('/apps/dieu/'+user+':'+system+'_status')
